const requireListIcons = [{
	icon: 'https://www.innovationgloble.com/wxs/img/wx/gkxcx/img/public/caigou.png'
}, {
	icon: 'https://www.innovationgloble.com/wxs/img/wx/gkxcx/img/public/yanfa.png'

}, {
	icon: 'https://www.innovationgloble.com/wxs/img/wx/gkxcx/img/public/jishuzixun.png'

}, {
	icon: 'https://www.innovationgloble.com/wxs/img/wx/gkxcx/img/public/other.png'
}];

const state = {
	requireList: [],
	techFields: [],
	currentTypeIndex: 0,
	selectTechFieldDesc:'',
	selectTechFieldIds:'',
	technicalFieldDescTips:'请选择', //页面显示 项
	tempFormData: null,
}

const actions = {
	loadTypes({
		commit,
		state
	}, payload) {
	}
};


const mutations = {
	saveFormData(state, formData){	
		state.tempFormData = formData;
	},
	saveTypes(state, list) {
		state.requireList = list.map(function(t, index) {
			return {
				title: t.desc,
				icon: requireListIcons[index % 4].icon,
				id:t.id
			}
		});
	},
	saveTechFields(state, list) {
		state.techFields = list.map(function(d, index){
			return {
				...d,
				selected:false
			}
		});
	},
	saveTypeIndex(state, index){
		state.currentTypeIndex = index;
	},
	updateTechFieldState(state, index){
		let techFields = state.techFields;
		const selectTechField = techFields[index];
		selectTechField.selected = !selectTechField.selected;
		techFields.splice(index,1,selectTechField);
		state.techFields = techFields;
		
		let selectTechFieldDesc= [];
		techFields.forEach(function(item, index){
			if(item.selected){
				selectTechFieldDesc.push(item.desc); 
			}
		});
		state.selectTechFieldDesc = selectTechFieldDesc.join(',');
		
		let selectTechFieldIds= [];
		techFields.forEach(function(item, index){
			if(item.selected){
				selectTechFieldIds.push(item.id); 
			}
		});
		state.selectTechFieldIds = selectTechFieldIds.join(',');
		
		
		// 页面显示项
		const descCount = selectTechFieldDesc.length;
		if(descCount > 0){
			state.technicalFieldDescTips = descCount > 1? (selectTechFieldDesc[0] + ',+'+(descCount-1)) : selectTechFieldDesc[0];
		}
		
	},
	resetTechFieldState(state, index){
		state.techFields = state.techFields.map(function(d, index){
			return {
				...d,
				selected:false
			}
		});
		
		state.selectTechFieldDesc = '';
		state.selectTechFieldIds = '';
		state.technicalFieldDescTips = '请选择'
	}
}

export default {
	namespaced: true,
	state,
	actions,
	mutations
}
